// pages/idex/index.js
let app = getApp();
import req from '../../utils/req';
import utils from '../../utils/utils'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiper: {
      indicatorDots: 'true',
      indicatorColor: '#fff',
      indicatorActiveColor: '#87C6FF',
      autoplay: 'true',
      interval: '3000',
      circular: 'true',
    },
    banner: [],
    newStrategy: [],
    introStrategy: [],
    refrash: false

  },

  goToSearch: function () {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  navToDetail(e) {
    wx.navigateTo({
      url: '../detail/detail?sid=' + e.currentTarget.dataset.sid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.refrash ? wx.showNavigationBarLoading() : '' //刷新加载
    this.getBanner()
    this.getNewStrategy()
    this.getIntroStrategy()
    if (this.data.refrash) {
      wx.hideNavigationBarLoading(); //完成停止加载图标
      wx.stopPullDownRefresh();
      utils.toast('success', '刷新成功', 500)
    }
  },

  getBanner() {
    req.get('加载中', '/img/selectBanners', '', (res) => {
      const list = res.data.list.filter(e => {
        return e.del_state === 0
      })
      this.setData({
        banner: list
      })
    })
  },
  search(type) {
    req.get('加载中', '/moment/getStrategy', type === 'introStrategy' ? {
      type: 1,
      delState: 1
    } : {
      delState: 1
    }, (res) => {
      const data = res.data
      data.list.forEach(e1 => {
        const images = []
        let taps = e1['taps'].split(',')
        e1['images'].forEach(e2 => {
          images.push(e2.url)
        })
        e1.images = images
        e1.taps = taps
        e1['createTime'] = utils.timeFormat(e1['createTime'])
      });
      const setData = type === 'newStrategy' ? {
        newStrategy: data.list
      } : {
        introStrategy: data.list
      }
      this.setData(setData)
    })
  },
  getNewStrategy() {
    this.search('newStrategy');
  },
  getIntroStrategy() {
    this.search('introStrategy');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      refrash: true
    })
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})